/**
 * Created by tronke on 2017/9/19.
 */
//**************1.browser-sync*********************

var SRC = 'src/*.js';
var DEST = 'src';

//1.通过 require 引入 gulp
var gulp = require("gulp");
//2.通过require引入 browsersync
var browserSync = require('browser-sync').create();

//加载gulp-autoprefixer模块
var autoprefixer = require('gulp-autoprefixer');
// Static server
gulp.task('browser-sync', function () {
    var files = [
        '**/*.html',
        '**/*.css',
        '**/*.js'
        // "./node_modules/**/*.js"
    ];
    browserSync.init({
        files: files,
        port: 8080,
        server: {
            baseDir: [DEST, './'],
            directory: true
        }
    })
});
//**************2.gulp-htmlmin*********************
var htmlmin = require("gulp-htmlmin");
gulp.task("html", function () {
    var options = {
        // removeComments: true,//清除HTML注释
        collapseWhitespace: true, //压缩HTML
        // collapseBooleanAttributes: true,//省略布尔属性的值 <input checked="true"/> ==> <input />
        // removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
        // removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
        // removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
        // minifyJS: true,//压缩页面JS
        // minifyCSS: true//压缩页面CSS
    };

    gulp.src(['src/**/*.html', 'src/**/*.htm'])
        .pipe(htmlmin(options))
        .pipe(gulp.dest(DEST))
});

//*****************3.gulp-imagemin******************
var imagemin = require("gulp-imagemin");
gulp.task("imagemin", function () {
    gulp.src('src/**/*.{jpg,png,gif,ico}')
        .pipe(imagemin())
        .pipe(gulp.dest(DEST))

})
//******************4.gulp-clean-css*****************
var Cssmin = require('gulp-clean-css');
gulp.task("minCss", function () {
    return gulp.src('./src/css/*.css')
        .pipe(cleanCSS({
            compatibility: 'ie8'
        }))
        .pipe(gulp.dest(DEST));
})
//确保已本地安装gulp-make-css-url-version [cnpm install gulp-make-css-url-version --save-dev]
cssver = require('gulp-make-css-url-version');

gulp.task('Cssmin', function () {
    gulp.src('src/css/*.css')
        .pipe(cssver()) //给css文件里引用文件加版本号（文件MD5）
        .pipe(Cssmin({
            compatibility: 'ie8'
        }))
        .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4')) //这个插件是用来加浏览器前缀的
        .pipe(gulp.dest(DEST + '/css'));
});
//****************5.gulp-uglify*******************
uglify = require('gulp-uglify');

gulp.task('jsmin', function () {
    //压缩src/js目录下的所有js文件
    //除了test1.js和test2.js（**匹配src/js的0个或多个子文件夹）
    gulp.src(['src/js/*.js', '!src/js/**/{test1,test2}.js'])
        .pipe(uglify())
        .pipe(gulp.dest(DEST + '/js'));
});


//***************6.gulp-less********************
less = require('gulp-less'),
    livereload = require('gulp-livereload');

gulp.task('less', function () {
    gulp.src('src/style/less/*.less')
        .pipe(less()) //编译less
        .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4')) //这个插件是用来加浏览器前缀的
        .pipe(gulp.dest(DEST + '/style/css')) // 生成目录
        .pipe(livereload()); // 编译重新加载
});

//特别注意：若编译less的时候，同时执行其他操作，有可能引起页面刷新，而不是将样式植入页面
//例如下面任务同时生成sourcemap：
//var sourcemaps = require('gulp-sourcemaps');
//gulp.task('less', function () {
//    gulp.src(['src/less/*.less'])
//        .pipe(sourcemaps.init())
//        .pipe(less())
//        .pipe(sourcemaps.write('./'))
//        .pipe(gulp.dest('src/css'))
//        .pipe(livereload());
//});

gulp.task('watch', function () {
    livereload.listen(); //监听，重新加载
    gulp.watch('src/style/less/**/*.less', ['less', 'browser-sync']);
    if (DEST != "src") {
        gulp.watch(['./**/*.html', './src/html/*.html'], ['html']);
    }
    gulp.watch('src/**/*.{jpg,png,gif,ico}', ["imagemin"]);

});

//***************7.gulp-connect********************
var connect = require('gulp-connect'); //包含服务器插件1
gulp.task('server', function () {
    connect.server({
        root: DEST, //服务器的根目录
        livereload: true //启用实时刷新的功能
    });
});



var changed = require('gulp-changed');
var ngmin = require('gulp-ngmin'); // just as an example



gulp.task('changed', function () {
    return gulp.src(SRC)
        .pipe(changed(DEST))
        // ngmin will only get the files that
        // changed since the last time it was run
        .pipe(ngmin())
        .pipe(gulp.dest(DEST));
});
/*
 *     ** 目录
 *     ！ 不包含
 *
 * */

if (DEST == "src") {
    //gulp.task('default', ['browser-sync','changed']);
    gulp.task('default', ["watch", 'changed', 'browser-sync', 'imagemin', 'Cssmin', 'less', 'jsmin', 'server']); //定义默认任务
} else {
    //gulp.task('default', ['browser-sync','changed']);
    gulp.task('default', ["watch", 'changed', 'browser-sync', 'html', 'imagemin', 'Cssmin', 'less', 'jsmin', 'server']); //定义默认任务
}